package com.huluobo.high3demo.demo;

import androidx.lifecycle.MutableLiveData;

import com.huluobo.mvvm.baen.UserBeanResult;
import com.huluobo.mvvm.net.ApiService;
import com.huluobo.mvvm.net.RetrofitManager;

import io.reactivex.Observer;
import io.reactivex.Scheduler;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by LC on 2023/9/14.
 */
public class LoginDemoRepo {
    public void login(String username, String password, MutableLiveData<UserBeanResult.UserBean> success,
                      MutableLiveData<String> failed) {
        //发起请求
        RetrofitManager.getInstance().getRetrofit().create(ApiService.class).login(username, password).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<UserBeanResult>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(UserBeanResult userBeanResult) {
                        //成功,给LiveData赋值
                        success.setValue(userBeanResult.getData());
                    }

                    @Override
                    public void onError(Throwable e) {
                        //失败,给LiveData赋值
                        failed.setValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
